package com.pic.others.mq;

import cn.hutool.dfa.SensitiveUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pic.common.constants.MqConstants;
import com.pic.common.constants.RedisConstants;
import com.pic.common.utils.R;
import com.pic.common.utils.SensitiveWordUtil;
import com.pic.common.utils.UserContext;
import com.pic.others.domain.po.Bottle;
import com.pic.others.domain.po.User;
import com.pic.others.mapper.BottleMapper;
import com.pic.others.mapper.SensitiveMapper;
import com.pic.others.mq.message.BottleComsumerMessage;
import com.pic.others.mq.message.BottleMessage;
import com.pic.others.service.IBottleService;
import com.pic.others.service.impl.SensitiveServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RequiredArgsConstructor
@Component
@Slf4j
public class BottleListerner {

    private final BottleMapper bottleMapper;
    private final IBottleService bottleService;
    private final StringRedisTemplate redisTemplate;
    private final SensitiveServiceImpl sensitiveService;
    private final SensitiveMapper sensitiveMapper;

//
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.bottle.queue1", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.BOTTLE_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.BOTTLE_HANDLER
    ))
    public void bottleHandler(BottleComsumerMessage comsumerMessage) {
        log.info("你走的是获取队列的rabbitmq");
        try {
            Long convertedBottleId = Long.parseLong(comsumerMessage.getBottleId());
            // 从Zset中删除漂流瓶的ID
            redisTemplate.opsForZSet().remove(RedisConstants.bottle.BOTTLE_ID, comsumerMessage.getBottleId());
            Bottle bottle = bottleService.getById(convertedBottleId);
            log.info("bottle:{}",bottle);
            if (bottle.getReceiveId() == null) {
                log.info("当前登录的用户是：{}",comsumerMessage);
                bottle.setReceiveId(Long.valueOf(comsumerMessage.getBottleId()));
                bottle.setEndTime(LocalDateTime.now());
            }
            bottleMapper.updateById(bottle);
        } catch (NumberFormatException e) {
            // 处理无法转换为Long的异常，可以记录日志或采取其他措施
            e.printStackTrace();
        }
    }



    /**
     * 瓶子敏感词
     * @param
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.bottle.queue2", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.BOTTLE_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.BOTTLESENSITIVE
    ))
    public void bottleSensitiveChecked(BottleMessage bottleMessage){
        log.info("你走的是检查敏感词队列的rabbitmq");
        Bottle bottle=new Bottle();
        log.info("当前登录的用户是：{}",UserContext.getUser());
        bottle.setBottleContent(bottleMessage.getContent());
        bottle.setSendId(Long.valueOf(bottleMessage.getUserId()));
        bottle.setCreateTime(LocalDateTime.now());
        log.info("bottle:{}",bottle);


        List<String> sensitives = sensitiveMapper.getAllSensitive();
        if(sensitives!=null){
            log.info("敏感词"+sensitives.get(0));
        }
        SensitiveWordUtil.initMap(sensitives);
        String content=bottle.getBottleContent();
        String replacedContent =SensitiveWordUtil.replaceSensitiveWords (content, "*");
        bottle.setBottleContent(replacedContent);
        log.info(replacedContent);
//        int insert = bottleMapper.insert(bottle);
//
//        log.info("bottle.getBottleId():{}",bottle.getBottleId());
        boolean save = bottleService.save(bottle);
        log.info("bottle.getBottleId():{}",bottle.getBottleId());
        if(save)
        {
            String key=String.valueOf(bottle.getBottleId());
            double randomWeight = ThreadLocalRandom.current().nextDouble();
            redisTemplate.opsForZSet().add(RedisConstants.bottle.BOTTLE_ID, key, randomWeight);
        }


    }
}
